package com.atguigu.lease.web.admin.shedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class SheduledTasks {
    @Autowired
    LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0 0 12 * * ?")
    public void checkLeaseStatus() {
        Date now = new Date();
        UpdateWrapper<LeaseAgreement> updateWrapper = new UpdateWrapper<>();
        updateWrapper.le("lease_end_date", now);
        updateWrapper.in("status", LeaseStatus.SIGNING, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING, LeaseStatus.RENEWING);
        updateWrapper.set("status", LeaseStatus.EXPIRED);
        leaseAgreementService.update(updateWrapper);
    }
}
